package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.entity.Brand;
import com.leyou.item.pojo.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    //自己写的品牌查询,返回品牌数据以及总页数  key= &page=2 &rows=5 &sortBy=id &desc=false
    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "key",required = false)String key,   //查询条件，可以没有
            @RequestParam(value = "page",defaultValue = "1")Integer page,  //当前页数，默认为1
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,  //每页的数据条数，默认为 5
            @RequestParam(value = "sortBy",defaultValue = "id")String sortBy,  //排序条件，默认以id排序
            @RequestParam(value = "desc",defaultValue = "false")Boolean desc  //是否降序
    ){
        return ResponseEntity.ok().body(brandService.queryBrandByPage(page,rows,key,sortBy,desc));
    }



    /*新增品牌*/
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids){
        /*  从前台拿到的数据：
            name: 黑马
            image:
            cids: 3,5
            letter: H
            */

        brandService.addBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /*修改品牌所属分类*/
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids){
        /*  从前台拿到的数据：
            name: 黑马
            image:
            cids: 3,5
            letter: H
            */

        brandService.updateBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //根据分类id查询品牌列表
    // /of/category?id=76
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandsByCategoryId(@RequestParam("id")Long categoryId){
        return ResponseEntity.ok().body(brandService.queryBrandsByCategoryId(categoryId));
    }

    //删除品牌
    @GetMapping("/delete/")
    public ResponseEntity<Boolean> deleteBrandById(@RequestParam("id")Long brandId){
        brandService.deleteBrandById(brandId);
        return ResponseEntity.ok().body(true);
    }

    /**
     * 根据品牌id查询品牌信息
     * @param brandId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long brandId){
        return ResponseEntity.ok().body(brandService.queryById(brandId));
    }

    /**
     * 根据品牌id的集合查询品牌集合
     * @param brandIds
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids")List<Long> brandIds){
        return ResponseEntity.ok().body(brandService.queryBrandByIds(brandIds));
    }

}
